/* 
 * @(#)MongoBaseDao.java 2013-5-21
 * Copyright (c) 2013 TianYu Technologies, Inc. All rights reserved.   
 * 
 * This software is the confidential and proprietary information of   
 * TianYu Technologies. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with TianYu Technologies.   
 *   
 */
package com.code.common.nosql;

import com.mongodb.BasicDBObject;

/**
 * 
 * Mongodb工具类
 * 
 * @author ����
 * @since 1.0
 * @version 2013-7-16 ����
 */
public class MongoDBQuery {

	/**
	 * query
	 */
	private final BasicDBObject queryAllConditon = new BasicDBObject();

	/**
	 * or
	 */
	private final BasicDBObject queryOrConditon = new BasicDBObject();

	/**
	 * order
	 */
	private final BasicDBObject order = new BasicDBObject();

	/**
	 * 初始化
	 * 
	 * @return
	 */
	public BasicDBObject getQueryConditon() {
		if (queryOrConditon.size() > 0) {
			queryAllConditon.put("$or", queryOrConditon);
		}
		return queryAllConditon.size() > 0 ? queryAllConditon : null;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public BasicDBObject getOrder() {
		return order.size() > 0 ? order : null;
	}

	/**
	 * 
	 * 
	 * @param parameter
	 * @param operate
	 * @param value ֵ
	 */
	public void addAndQuery(String parameter, String operate, Object value) {
		if ("=".equals(operate)) {
			queryAllConditon.put(parameter, value);
		} else {
			queryAllConditon.put(parameter, transQuery(parameter, operate, value));
		}
	}

	/**
	 * 
	 * 
	 * @param parameter
	 * @param operate
	 * @param value ֵ
	 */
	public void addOrQuery(String parameter, String operate, Object value) {
		if ("=".equals(operate)) {
			queryOrConditon.put(parameter, value);
		} else {
			queryOrConditon.put(parameter, transQuery(parameter, operate, value));
		}
	}

	/**
	 * 
	 * 
	 * @param parameter
	 * @param value ֵ
	 */
	public void addOrder(String parameter, String value) {
		order.put(parameter, "ASC".equals(value.toUpperCase()) ? 1 : -1);
	}

	/**
	 * 
	 * 
	 * @Date 2013-11-19
	 * @author 李鹏
	 * @param parameter
	 * @param operate
	 * @param value
	 * @return
	 */
	private BasicDBObject transQuery(String parameter, String operate, Object value) {
		BasicDBObject objResult = null;
		if (">".equals(operate)) {
			objResult = new BasicDBObject("$gt", value);
		} else if ("<".equals(operate)) {
			objResult = new BasicDBObject("$lt", value);
		} else if (">=".equals(operate)) {
			objResult = new BasicDBObject("$gte", value);
		} else if ("<=".equals(operate)) {
			objResult = new BasicDBObject("$lte", value);
		} else if ("!=".equals(operate)) {
			objResult = new BasicDBObject("$ne", value);
		} else if ("nin".equals(operate)) {
			objResult = new BasicDBObject("$nin", value);
		} else if ("in".equals(operate)) {
			objResult = new BasicDBObject("$in", value);
		} else if ("size".equals(operate)) {
			objResult = new BasicDBObject("$size", value);
		} else if ("exists ".equals(operate)) {
			objResult = new BasicDBObject("$exists", value);
		}
		return objResult;
	}
}
